<?php

namespace app\controller;

use app\BaseController;

class Index extends BaseController
{
    public function index()
    {
        phpinfo();
        return '3412';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }


    public function wxLogin()
    {
        $data = request()->param();
        $wxHelper = new \WxHelper();
        $result = $wxHelper->WxDecode($data);
        return json($result);
    }

    /**
     * 微信支付
     *  1 创建预支付订单
     *      调用第三方库 createOrder()
     *  2 调用支付 (小程序客户端发起)
     *      wx.requestPayment({
     *           timeStamp: '',
     *          nonceStr: '',
     *          package: '',
     *          signType: 'RSA', // MD5或RSA（根据返回使用）
     *          paySign: '',
     *          success (res) { },
     *          fail (res) { }
     *      })
     *  3 接受支付回调
     *      payCb
     */
    public function createOrder()
    {
        // {title:'',money:'',openid:''}
        $data = request()->param();
        $order = [
            'body' => $data['title'],
            'order_sn' => time() + random_int(1000,9999), // 商户订单号
            'total_amount' => $data['money'], // 订单金额
            'openid' => $data['openid'], // 用户唯一标识
        ];
        $wxHelper = new \WxHelper();
        $result = $wxHelper->makeOrder($order);
        return json($result);
    }


    public function payCb()
    {
        $wxHelper = new \WxHelper();
        $result = $wxHelper->getCb();
        if ($result['status'] == 'ok') {
            //支付成功  打印支付参数
            dump($result['data']);
            //更改订单支付状态
        } else {
            //支付错误处理
            dump($result);
        }

    }

}
